<%--
  Created by IntelliJ IDEA.
  User: HW
  Date: 2018/4/19
  Time: 1:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<script type="text/javascript" src="../../js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="../../js/jquery-migrate-1.2.1.js"></script>
<script src="../../js/jquery.js"></script>
<head>
    <title>乡镇图书管理系统</title>
</head>
<body style="background:cornflowerblue">
<div align="center">
    <table style="width: auto" border="1">
        <thead>
        <tr>
            <td align="center" colspan="7">
                <h1>乡镇图书管理系统</h1>
            </td>
        </tr>
        </thead>
        <tr id="gnbt">
            <td id="xxtd1" width="300px" onmouseover="changecolor(this.id)" onclick="cxts()"><a href="#"
                                                                                                style="text-decoration: none;color: #004999">图书信息管理</a>
            </td>
            <td id="xxtd2" width="200px" onmouseover="changecolor(this.id)" onclick="cxtype()"><a href="#"
                                                                                                  style="text-decoration: none;color: #004999">图书类型管理</a>
            </td>
            <td id="xxtd3" width="150px" onmouseover="changecolor(this.id)" onclick="cxinout()"><a href="#"
                                                                                                   style="text-decoration: none;color: #004999">图书归借管理</a>
            </td>
            <td align="right" colspan="4"><input type="text" id="bookName"><input type="button" value="搜索"
                                                                                  onclick="searchBook()"></td>
        </tr>
        <tr id="bookbt">
        </tr>
        <tbody id="tsxx">
        </tbody>
        <tr id="pagetd">
            <td style="text-align: right" colspan="7">
                <button id="addBook" style="float: left;margin-left: 10px" onclick="addBookOrType()">增加图书</button>
                第<select id="page" onchange="pageswap(this.id)"></select>页
        </tr>
    </table>
</div>
</body>
<script type="text/javascript">
    var xxtd1;
    var pagenum = 1;
    var databook;
    var datatype;
    var serachtype = 1;

    function changecolor(id) {
        if (xxtd1 == null) {
            xxtd1 = document.getElementById(id);
            xxtd1.style.backgroundColor = 'steelblue';
        } else {
            xxtd1.style.backgroundColor = 'cornflowerblue';
            xxtd1 = document.getElementById(id);
            xxtd1.style.backgroundColor = 'steelblue';
        }
    };

    window.onload = function () {
        if (serachtype == 1) {
            cxts();
        }
        if (serachtype == 2) {
            cxtype();
        }
    };

    function cxts() {
        $.ajax({
            type: "post",
            url: "/book/getBookList",
            dataType: "json",
            data: {},
            success: function (data) {
                $("#addBook").text("增加图书");
                serachtype = 1;
                databook = null;
                databook = data;
                $("#tsxx").html("");
                addtsbt();
                var j = 1;
                for (var i = 0; i < data.length; i++) {
                    if (j == 21) {
                        break;
                    }
                    var tr = $("<tr/>");
                    var td1 = $("<td/>");
                    var td2 = $("<td/>");
                    var td3 = $("<td/>");
                    var td4 = $("<td/>").addClass("cz");
                    td1.append(data[i].bookName);
                    td2.append(data[i].typeName);
                    td3.append(data[i].author);
                    if ('<%=request.getAttribute("roel")%>' == 1) {
                        var a1 = $("<button/>");
                        var a2 = $("<button/>");
                        a1.attr({class: "upbook", onclick: "updatebook(" + data[i].id + ")"});
                        a1.append("修改");
                        a2.attr({class: "upbook", onclick: "deletebook(" + data[i].id + ")"});
                        a2.append("删除");
                        a2.appendTo(td4);
                        a1.appendTo(td4);
                    }
                    td1.appendTo(tr);
                    td2.appendTo(tr);
                    td3.appendTo(tr);
                    td4.appendTo(tr);
                    tr.appendTo($("#tsxx"));
                    j++;
                }
                pagenum = (data.length / 20) + 1;
                $("#page").html("");
                for (var j = 1; j <= pagenum; j++) {
                    var option = $("<option/>");
                    option.append(j);
                    option.appendTo($("#page"));
                }
            },
            error: function (request) {
            }
        })
    }

    function cxtype() {
        $.ajax({
            type: "post",
            url: "/book/getBookTypeList",
            dataType: "json",
            data: {},
            success: function (data) {
                $("#addBook").text("增加类型");
                serachtype = 2;
                datatype = data;
                $("#tsxx").html("");
                addtypebt();
                var j = 1;
                for (var i = 0; i < data.length; i++) {
                    if (j == 21) {
                        break;
                    }
                    var tr = $("<tr/>");
                    var td1 = $("<td/>");
                    var td5 = $("<td/>").addClass("cz");
                    td1.append(data[i].typeName);
                    if ('<%=request.getAttribute("roel")%>' == 1){
                    var a1 = $("<button/>");
                    var a2 = $("<button/>");
                    a1.attr({class: "upbook", onclick: "updatetype(" + data[i].id + ")"});
                    a1.append("修改");
                    a2.attr({class: "upbook", onclick: "deleteType(" + data[i].id + ")"});
                    a2.append("删除");
                    a2.appendTo(td5);
                    a1.appendTo(td5);}
                    td1.appendTo(tr);
                    td5.appendTo(tr);
                    tr.appendTo($("#tsxx"));
                    j++;
                }
                pagenum = (data.length / 20) + 1;
                $("#page").html("");
                for (var j = 1; j <= pagenum; j++) {
                    var option = $("<option/>");
                    option.append(j);
                    option.appendTo($("#page"));
                }
            },
            error: function (request) {

            }
        })
    }

    function cxinout() {
        $.ajax({
            type: "post",
            url: "/book/getBookList",
            dataType: "json",
            data: {},
            success: function (data) {
                $("#addBook").text("增加图书");
                serachtype = 3;
                databook = null;
                databook = data;
                $("#tsxx").html("");
                addinoutbt();
                var j = 1;
                for (var i = 0; i < data.length; i++) {
                    if (j == 21) {
                        break;
                    }
                    var tr = $("<tr/>");
                    var td1 = $("<td/>");
                    var td2 = $("<td/>");
                    var td3 = $("<td/>");
                    var td4 = $("<td/>");
                    var td5 = $("<td/>");
                    var td6 = $("<td/>");
                    var td7 = $("<td/>").addClass("cz");
                    td1.append(data[i].bookName);
                    td2.append(data[i].typeName);
                    td3.append(data[i].author);
                    if (data[i].state == 0) {
                        td4.append("是");
                    } else {
                        td4.append("否");
                    }
                    td5.append(data[i].outDate);
                    td6.append(data[i].inDate);
                    if ('<%=request.getAttribute("roel")%>' == 1){
                    var a1 = $("<button/>");
                    var a2 = $("<button/>");
                    a1.attr({class: "upbook", onclick: "outbook(" + data[i].id + "," + data[i].state + ")"});
                    a1.append("借出");
                    a2.attr({class: "upbook", onclick: "inbook(" + data[i].id + "," + data[i].state + ")"});
                    a2.append("归还");
                    a2.appendTo(td7);
                    a1.appendTo(td7);
                    }
                    td1.appendTo(tr);
                    td2.appendTo(tr);
                    td3.appendTo(tr);
                    td4.appendTo(tr);
                    td5.appendTo(tr);
                    td6.appendTo(tr);
                    td7.appendTo(tr);
                    tr.appendTo($("#tsxx"));
                    j++;
                }
                pagenum = (data.length / 20) + 1;
                $("#page").html("");
                for (var j = 1; j <= pagenum; j++) {
                    var option = $("<option/>");
                    option.append(j);
                    option.appendTo($("#page"));
                }
            },
            error: function (request) {
            }
        });
    }

    function pageswap() {
        if (serachtype == 1) {
            $("#tsxx").html("");
            addtsbt();
            var j = 1;
            var pagenum = $("#page").val();
            for (var i = (pagenum - 1) * 20; i < databook.length; i++) {
                if (j == 21) {
                    break;
                }
                var tr = $("<tr/>");
                var td1 = $("<td/>");
                var td2 = $("<td/>");
                var td3 = $("<td/>");
                var td4 = $("<td/>").addClass("cz");
                td1.append(databook[i].bookName);
                td2.append(databook[i].typeName);
                td3.append(databook[i].author);
                if ('<%=request.getAttribute("roel")%>' == 1){
                var a1 = $("<button/>");
                var a2 = $("<button/>");
                a1.attr({class: "upbook", onclick: "updatebook(" + databook[i].id + ")"});
                a1.append("修改");
                a2.attr({class: "upbook", onclick: "deletebook(" + databook[i].id + ")"});
                a2.append("删除");
                a2.appendTo(td4);
                a1.appendTo(td4);}
                td1.appendTo(tr);
                td2.appendTo(tr);
                td3.appendTo(tr);
                td4.appendTo(tr);
                tr.appendTo($("#tsxx"));
                j++;
            }
        }
        if (serachtype == 2) {
            $("#tsxx").html("");
            addtsbt();
            var j = 1;
            var pagenum = $("#page").val();
            for (var i = (pagenum - 1) * 20; i < datatype.length; i++) {
                if (j == 21) {
                    break;
                }
                var tr = $("<tr/>");
                var td1 = $("<td/>");
                var td5 = $("<td/>").addClass("cz");
                td1.append(datatype[i].typeName);
                if ('<%=request.getAttribute("roel")%>' == 1){
                var a1 = $("<button/>");
                var a2 = $("<button/>");
                a1.attr({class: "upbook", onclick: "updatetype(" + datatype[i].id + ")"});
                a1.append("修改");
                a2.attr({class: "upbook", onclick: "deleteType(" + datatype[i].id + ")"});
                a2.append("删除");
                a2.appendTo(td5);
                a1.appendTo(td5);}
                td1.appendTo(tr);
                td5.appendTo(tr);
                tr.appendTo($("#tsxx"));
                j++;
            }

        }
        if (serachtype == 3) {
            $("#tsxx").html("");
            addinoutbt();
            var j = 1;
            var pagenum = $("#page").val();
            for (var i = (pagenum - 1) * 20; i < databook.length; i++) {
                if (j == 21) {
                    break;
                }
                var tr = $("<tr/>");
                var td1 = $("<td/>");
                var td2 = $("<td/>");
                var td3 = $("<td/>");
                var td4 = $("<td/>");
                var td5 = $("<td/>");
                var td6 = $("<td/>");
                var td7 = $("<td/>").addClass("cz");
                td1.append(databook[i].bookName);
                td2.append(databook[i].typeName);
                td3.append(databook[i].author);
                if (databook[i].state == 0) {
                    td4.append("是");
                } else {
                    td4.append("否");
                }
                td5.append(databook[i].outDate);
                td6.append(databook[i].inDate);
                if ('<%=request.getAttribute("roel")%>' == 1){
                var a1 = $("<button/>");
                var a2 = $("<button/>");
                a1.attr({class: "upbook", onclick: "outbook(" + databook[i].id + ")"});
                a1.append("借出");
                a2.attr({class: "upbook", onclick: "inbook(" + databook[i].id + ")"});
                a2.append("归还");
                a2.appendTo(td7);
                a1.appendTo(td7);}
                td1.appendTo(tr);
                td2.appendTo(tr);
                td3.appendTo(tr);
                td4.appendTo(tr);
                td5.appendTo(tr);
                td6.appendTo(tr);
                td7.appendTo(tr);
                tr.appendTo($("#tsxx"));
                j++;
            }
        }
    }

    function searchBook() {
        if (serachtype == 1) {
            $.ajax({
                type: "post",
                url: "/book/getBookListByName",
                dataType: "json",
                data: {
                    bookName: $("#bookName").val()
                },
                success: function (data) {
                    databook = null;
                    databook = data;
                    addtsbt();
                    $("#tsxx").html("");
                    var j = 1;
                    for (var i = 0; i < data.length; i++) {
                        if (j == 21) {
                            break;
                        }
                        var tr = $("<tr/>");
                        var td1 = $("<td/>");
                        var td2 = $("<td/>");
                        var td3 = $("<td/>");
                        var td4 = $("<td/>").addClass("cz");
                        td1.append(data[i].bookName);
                        td2.append(databook[i].typeName);
                        td3.append(databook[i].author);
                        if ('<%=request.getAttribute("roel")%>' == 1){
                        var a1 = $("<button/>");
                        var a2 = $("<button/>");
                        a1.attr({class: "upbook", onclick: "updatebook(" + databook[i].id + ")"});
                        a1.append("修改");
                        a2.attr({class: "upbook", onclick: "deletebook(" + databook[i].id + ")"});
                        a2.append("删除");
                        a2.appendTo(td4);
                        a1.appendTo(td4);}
                        td1.appendTo(tr);
                        td2.appendTo(tr);
                        td3.appendTo(tr);
                        td4.appendTo(tr);
                        tr.appendTo($("#tsxx"));
                        j++;
                    }
                    pagenum = (data.length / 20) + 1;
                    $("#page").html("");
                    for (var j = 1; j <= pagenum; j++) {
                        var option = $("<option/>");
                        option.append(j);
                        option.appendTo($("#page"));
                    }
                }
            });
        }
        if (serachtype == 2) {
            $.ajax({
                type: "post",
                url: "/book/getTypeListByName",
                dataType: "json",
                data: {
                    bookName: $("#bookName").val()
                },
                success: function (data) {
                    datatype = null;
                    datatype = data;
                    addtypebt();
                    $("#tsxx").html("");
                    var j = 1;
                    for (var i = 0; i < data.length; i++) {
                        if (j == 21) {
                            break;
                        }
                        var tr = $("<tr/>");
                        var td1 = $("<td/>");
                        var td5 = $("<td/>").addClass("cz");
                        td1.append(data[i].typeName);
                        if ('<%=request.getAttribute("roel")%>' == 1){
                        var a1 = $("<button/>");
                        var a2 = $("<button/>");
                        a1.attr({class: "upbook", onclick: "updatebook(" + databook[i].id + ")"});
                        a1.append("修改");
                        a2.attr({class: "upbook", onclick: "deletebook(" + databook[i].id + ")"});
                        a2.append("删除");
                        a2.appendTo(td5);
                        a1.appendTo(td5);}
                        td1.appendTo(tr);
                        td5.appendTo(tr);
                        tr.appendTo($("#tsxx"));
                        j++;
                    }
                    pagenum = (data.length / 20) + 1;
                    $("#page").html("");
                    for (var j = 1; j <= pagenum; j++) {
                        var option = $("<option/>");
                        option.append(j);
                        option.appendTo($("#page"));
                    }
                }
            });
        }
        if (serachtype == 3) {
            $.ajax({
                type: "post",
                url: "/book/getBookListByName",
                dataType: "json",
                data: {
                    bookName: $("#bookName").val()
                },
                success: function (data) {
                    $("#addBook").text("增加图书");
                    serachtype = 3;
                    databook = null;
                    databook = data;
                    $("#tsxx").html("");
                    addinoutbt();
                    var j = 1;
                    for (var i = 0; i < data.length; i++) {
                        if (j == 21) {
                            break;
                        }
                        var tr = $("<tr/>");
                        var td1 = $("<td/>");
                        var td2 = $("<td/>");
                        var td3 = $("<td/>");
                        var td4 = $("<td/>");
                        var td5 = $("<td/>");
                        var td6 = $("<td/>");
                        var td7 = $("<td/>").addClass("cz");
                        td1.append(data[i].bookName);
                        td2.append(data[i].typeName);
                        td3.append(data[i].author);
                        if (data[i].state == 0) {
                            td4.append("是");
                        } else {
                            td4.append("否");
                        }
                        td5.append(data[i].outDate);
                        td6.append(data[i].inDate);
                        if ('<%=request.getAttribute("roel")%>' == 1){
                        var a1 = $("<button/>");
                        var a2 = $("<button/>");
                        a1.attr({class: "upbook", onclick: "outbook(" + data[i].id + ")"});
                        a1.append("借出");
                        a2.attr({class: "upbook", onclick: "inbook(" + data[i].id + ")"});
                        a2.append("归还");
                        a2.appendTo(td7);
                        a1.appendTo(td7);}
                        td1.appendTo(tr);
                        td2.appendTo(tr);
                        td3.appendTo(tr);
                        td4.appendTo(tr);
                        td5.appendTo(tr);
                        td6.appendTo(tr);
                        td7.appendTo(tr);
                        tr.appendTo($("#tsxx"));
                        j++;
                    }
                    pagenum = (data.length / 20) + 1;
                    $("#page").html("");
                    for (var j = 1; j <= pagenum; j++) {
                        var option = $("<option/>");
                        option.append(j);
                        option.appendTo($("#page"));
                    }
                },

            });
        }
    }

    function addtsbt() {
        $("#bookbt").html("");
        var td1 = $("<td/>");
        var td2 = $("<td/>");
        var td3 = $("<td/>");
        var td4 = $("<td/>");
        td1.addClass("bt");
        td2.addClass("bt");
        td3.addClass("bt");
        td4.addClass("bt");
        td1.append("图书名称");
        td1.appendTo($("#bookbt"));
        td2.append("图书类型")
        td2.appendTo($("#bookbt"));
        td3.append("作者");
        td3.appendTo($("#bookbt"));
        td4.append("操作");
        td4.appendTo($("#bookbt"));
    }

    function addtypebt() {
        $("#bookbt").html("");
        var td1 = $("<td/>");
        var td5 = $("<td/>");
        td1.addClass("bt");
        td5.addClass("bt");
        td1.append("图书类型");
        td1.appendTo($("#bookbt"));
        td5.append("操作");
        td5.appendTo($("#bookbt"));
    }

    function addinoutbt() {
        $("#bookbt").html("");
        var td1 = $("<td/>");
        var td2 = $("<td/>");
        var td3 = $("<td/>");
        var td4 = $("<td/>");
        var td5 = $("<td/>");
        var td6 = $("<td/>");
        var td7 = $("<td/>");
        td1.addClass("bt");
        td2.addClass("bt");
        td3.addClass("bt");
        td4.addClass("bt");
        td5.addClass("bt");
        td6.addClass("bt");
        td7.addClass("bt");
        td1.append("图书名称");
        td1.appendTo($("#bookbt"));
        td2.append("图书类型")
        td2.appendTo($("#bookbt"));
        td3.append("作者");
        td3.appendTo($("#bookbt"));
        td4.append("是否借出");
        td4.appendTo($("#bookbt"));
        td5.append("借出日期");
        td5.appendTo($("#bookbt"));
        td6.append("归还日期");
        td6.appendTo($("#bookbt"));
        td7.append("操作");
        td7.appendTo($("#bookbt"));
    }

    function deletebook(bookId) {
        var ifdelete = confirm("确定要删除吗？");
        if (ifdelete) {
            $.ajax({
                type: "post",
                url: "/book/deleteBookById",
                dataType: "json",
                data: {
                    "bookId": bookId
                },
                success: function (data) {
                    if (data == 1) {
                        alert("删除成功");
                        cxts();
                    } else {
                        alert("删除失败")
                    }
                }
            })
        }
    }

    function deleteType(typeId) {
        var ifdelete = confirm("确定要删除吗？");
        if (ifdelete) {
            $.ajax({
                type: "post",
                url: "/book/deleteTypeById",
                dataType: "json",
                data: {
                    "typeId": typeId,
                },
                success: function (data) {
                    if (data == 1) {
                        alert("删除成功");
                        cxtype();
                    } else {
                        alert("删除失败")
                    }
                }
            })
        }
    }

    function addBookOrType() {
        if ('<%=request.getAttribute("roel")%>' == 1) {
        if (serachtype == 1) {
            window.location.href = "${pageContext.request.contextPath}/book/toAddBookPage"
        } else {
            window.location.href = "${pageContext.request.contextPath}/book/toAddTypePage"
        }}else {
            alert("你不是管理员，无法添加")
        }
    }

    function updatebook(bookId) {
        window.location.href = "${pageContext.request.contextPath}/book/toUpdateBookPage?bookId=" + bookId;
    }

    function updatetype(typeId) {
        window.location.href = "${pageContext.request.contextPath}/book/toUpdateTypePage?typeId=" + typeId;
    }

    function outbook(bookId, state) {
        if (state != 0) {
            window.location.href = "${pageContext.request.contextPath}/book/toOutBookPage?bookId=" + bookId;
        } else {
            alert("已借出，请选择其他图书")
        }
    }

    function inbook(bookId, state) {
        if (state != 1) {
            if (confirm("确定归还？")) {
                $.ajax({
                    type: "post",
                    url: "/book/inBook",
                    dataType: "json",
                    data: {
                        bookId: bookId
                    },
                    success: function (data) {
                        if (data == 1) {
                            alert("归还成功");
                            cxinout();
                        } else {
                            alert("归还失败")
                        }
                    }
                })
            }

        } else {
            alert("这本书还没借出，无需归还");
        }
    }

</script>
<style>
    .upbook {
        margin-right: 10px;
        text-decoration: none
    }

    .cz {
        text-align: center;
    }

    .bt {
        text-align: center;
    }
</style>
</html>
